// MathematicaTranslator.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>
using std::cout; using std::cerr; using std::endl; using std::cin;

#include "Scanner.h"
using MathematicaTranslator::Scanner;

#include "MTDriver.hh"
#include "CodeGenContext.hpp"

extern MathematicaTranslator::MTBlock* programBlock;

int main(int argc, char* argv[])
{	
	enum PARSE_RESULTS { FAIL = 0, SUCCESS = 1 };

	MathematicaTranslator::MTDriver driver;
	bool readfile = false;
	int parseResult = FAIL;

	for (int argi = 1; argi < argc; ++argi){
		if (argv[argi] == std::string("-p"))
		{
			driver.traceParsing = true;
		}
		else if (argv[argi] == std::string("-s"))
		{
			driver.traceScanning = true;
		}
		else {
			std::fstream infile(argv[argi]);
			if (!infile.good()){
				cerr << "Could not open file: " << argv[1] << endl;
				return 0;
			}

			parseResult = driver.parse_stream(infile,argv[1]);
			cout<< parseResult <<endl;
		}
		readfile = true;
	}

	if (readfile && parseResult == SUCCESS) 
	{
		//AST processing
		MathematicaTranslator::CodeGenContext context("generated_calculations.cu");
		context.generateCode(*programBlock);

		return 0;
	} else if( readfile && parseResult == FAIL )
	{
		cerr << "PARSING FAILURE" << endl;
		return 1;
	}
    cout << "Reading expressions from stdin" << endl;

    std::string line;
    while( cout << "input: " && std::getline(cin, line) && !line.empty()) {
		cout << driver.parse_string(line, "input") << endl;
	}
}


